<?php

class Eclib {

	public function joinrefercode(){
		
		while(1){
			$refercode = createrefercode();
			$sql = "select my_refer from ebs_member where my_refer = '$refercode'";
			$res = mysql_query($sql);
			$data= mysql_num_rows($res);
			if(!$data){
				break;
			}
		}
		return $refercode;

	}

	public function createrefercode(){

		$chars = range('a', 'z');
		$nums = range(0, 9);

		shuffle($chars);
		shuffle($nums);

		$rand_chars = implode(array_slice($chars, 0, 3));
		$rand_nums = implode(array_slice($nums, 0, 3));

		return $rand_chars.$rand_nums;
	}

	//회복비용
	public function _getRecoveryPay_($data){
		if($data['level']>=15){
			if($data['hp'] == 0){
				return (int)(($data['max_hp']-$data['hp']) * 0.9);
			}else{
				return (int)(($data['max_hp']-$data['hp']) * 0.4);
			}
		}else{
			return 0;
		}	
	}

	//최대HP 증가 비용
	public function _getMaxHpPay_($data){
		return (int)($data['max_hp'] * 2.1);
	}
	//최대EN 증가 비용
	public function _getMaxEnPay_($data){
		return 1;
		//return (int)($data[max_en] * $data[max_en] * 0.04);
	}

	//레벨별 최대 EN 공식
	public function _getMaxEn_($level){
		return 100 + $level * 0.5;
	}

	//전투 당 획득 돈
	public function _getMoney_($level, $enemy_level){
		$m = $level*0.8;	
		$m *= max( 30 - abs($enemy_level - $level), 0)/30;
		$m += 30;
		return (int)$m;
	}

	//전투 당 획득 경험치
	public function _getBattleExp_($hp, $enemy_hp, $level, $enemy_level){
		$exp = max( 30 - abs($enemy_level - $level), 0);
		$exp += max( ($enemy_level - $level)*0.5, 0);
		//저랩 사냥때
		if($enemy_level < $level){
			$exp *= 0.8;
		}
		
		if($exp < 1){
			$exp = 1;
		}
		
		//전사 시키면 5배
		if($enemy_hp <= 0){
			$exp *= 5;
		}

		return (int)$exp;
		
		/*
		//레벨이 비슷할 수록 경험치 획득 량 높음
		// 렙차: 20 ~ 0
		// EXP : 1 ~ 100
		$result =( 20 - abs($enemy_level - $level) ) * 5;
		
		//저랩 사냥때는 1/2 만 획득
		if($level > $enemy_level){
			$result *= 0.5;
		}
		
		return (int)$result;
		*/
	}

	//레벨별 최대 경험치 공식
	public function _getMaxExp_($level){
		return $level * 36 + $level * $level;
	}

	//건국 비용
	public function _get_nation_create_play_(){
		$sql = "SELECT count(*) FROM ebs_nation";
		$res = mysql_query($sql);
		$nation_count_data = mysql_fetch_array($res);
		return ($nation_count_data[0]+1) * 1300;
	}

	public function _getOkPayMoon_($data){
		return $data['cash'] >= $pay_moon;
	}

	public function _getMoonCount_($data){
		return 15*60 - (time()-$data['timestemp_moon']);
	}

	public function stat_conv($data){

		$stat_info = array();
		//스텟 불러오기
		$atk = max(0, $data['sp_atk']);
		$hit = max(0, $data['sp_hit']);
		$def = max(0, $data['sp_def']);
		$spd = max(0, $data['sp_spd']);
		//스텟 레벨보정
		$stat_max = $data['level'] * $stat_levelup * 0.5;
		$stat_info['atk'] = $stat_max/4 + $atk*0.5;
		$stat_info['hit'] = $stat_max/4 + $hit*0.5;
		$stat_info['def'] = $stat_max/4 + $def*0.5;
		$stat_info['spd'] = $stat_max/4 + $spd*0.5;

		return $stat_info;
	}
}